<template>
    <div>
        <!-- 标题导航组件 -->
        <navtitle title="登陆"></navtitle>
        <div style="width: 100%; margin: 20px 0;">
            <img src="/src/images/log_03.gif" alt="" style="width: 50%; margin-left: 24%;">
        </div>
        <!-- 表单 -->
        <van-form @submit="onSubmit">
            <van-cell-group inset>
                <!-- 手机号 -->
                <van-field v-model="username" name="username" left-icon="phone&emsp;" placeholder="&emsp;手机号"
                    :rules="[{ pattern, message: '请填写手机号' }]" />
                <div style="height: 30px;"></div>
                <!-- 密码 -->
                <van-field v-model="password" type="password" name="password" left-icon="lock&emsp;"
                    placeholder="&emsp;密码" :rules="[{ required: true, message: '请填写密码' }]" />
                <div style="height: 50px;"></div>
            </van-cell-group>
            <div style="margin: 16px;">
                <van-button block type="primary" native-type="submit" style="background-color:#f04864; border: none;">
                    登陆
                </van-button>
                <p @click="toReg">立即注册</p>
            </div>
        </van-form>
    </div>
</template>

<script setup>
// 标题导航组件
import navtitle from '@/components/navtitle.vue';

const username = ref('');
const password = ref('');

// 手机号正则
let pattern = /^1[3-9]\d{9}$/

// 引入接口
import { useRouter } from 'vue-router';
const router = useRouter()
import { ref } from 'vue';
import { loginApi } from '@/api/api';
import { showFailToast, showSuccessToast } from 'vant';
const onSubmit = () => {
    loginApi(username.value, password.value).then((res) => {
        console.log(res);
        if (password.value.length > 8) {
            showFailToast('密码长度大于8位登录失败')
        } else {
            if (res.data.code == 0) {
                showSuccessToast('登陆成功')
                localStorage.setItem('token', res.data.data.token)
                localStorage.setItem('username', res.data.data.username)
                router.push('/home/my')
            } else {
                showFailToast('登陆失败')
            }
        }

    })

}
const toReg = () => {
    router.push('/reg')
}
</script>

<style lang="scss" scoped></style>